        /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import javax.sql.RowSetInternal;
import javax.sql.rowset.WebRowSet;
import javax.sql.rowset.spi.XmlReader;
import javax.xml.transform.Source;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.xml.xpath.Jaxp13XPathTemplate;
import org.springframework.xml.xpath.NodeMapper;
import org.jdom.*;
import org.jdom.Document;
import org.jdom.input.*;

/**
 *
 * @author Administrador
 */
public class NexusRestAccess {

    private static Logger logger = LoggerFactory.getLogger(NexusRestAccess.class);

    public String getNexusUsuario() {
        String Xml = "";
        try {
            InputStream is = get("http://192.168.1.101:8080/hayTourViajes/rest/usuario/fatima");
            Xml = convertStreamToString(is);
            // AQUI tengo el XML ahora con DOm debo parcearlo por tag y mostrarlo por interfaz.
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(NexusRestAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Xml.toString();
    }

    public String convertStreamToString(InputStream is)
            throws IOException {
        /*
         * To convert the InputStream to String we use the
         * Reader.read(char[] buffer) method. We iterate until the
         * Reader return -1 which means there's no more data to
         * read. We use the StringWriter class to produce the string.
         */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }

    public static InputStream get(String urlStr) throws Exception {
        return readFrom(urlStr, null);
    }

    public static InputStream post(String urlStr, String post)
            throws Exception {
        return readFrom(urlStr, post);
    }

    private static InputStream readFrom(String urlStr, String postStr) throws MalformedURLException, IOException {
        URLConnection conn = new URL(urlStr).openConnection();
        conn.setDoInput(true);
        if (postStr != null && postStr.length() > 0) {
            conn.setDoOutput(true);
            DataOutputStream output =
                    new DataOutputStream(conn.getOutputStream());
            output.writeBytes(postStr);
            output.flush();
            output.close();
        }
        return conn.getInputStream();
    }

    public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException {
       HacerConsumo d = new HacerConsumo();
     List Y = d.ConsumoViaje("gzerpa");
     //   System.out.println("coYACA YACAAAAA " + ((Viaje) Y.get(1)).getSitioAlojamiento());
     //
//       List Y2 = d.ConsumoItinerario("1","jperez");
     //   System.out.println("coYACA YACgggggA " + ((Itinerario) Y2.subList(0,1)).getSitioAlojamiento());
//        int i =0;
//        Itinerario It;
//        List Pa;
//        List Ac;
//        It = (Itinerario) Y2.get(0);
//        Pa = It.getParticipante();
//        Ac = It.getActividad();
//        System.out.println("hola"+It.getLugarDestino());
////       for (int j=0;j<It.size();j++){
//           System.out.println("iu"+It.get(j));
//       It.set(i,Y2.get(i));
//       for (int h=0;h<Y2.size();h++){
//
//       }
        //}
//        while(i == Y2.listIterator()){
//           Y2.listIterator()
////        It.add(i,((Itinerario) Y2.get(i)));
//         System.out.println("FECHAAAA " + ((Actividad) ((Itinerario) Y2.get(i)).getActividad().get(i+1)).getFechaInicio());
//        i++; }
      
    }
}
